package gw.com.android.ui.news;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.presenter.http.FlashRequest;

/**
 * 快讯适配器
 *
 * @author jett
 * @since 2017-11-23.
 */
public class FlashAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity mContext;
    private List<FlashRequest.FlashData> mData;
    private String[] weekDays;

    public FlashAdapter(Activity context) {
        this.mContext = context;
        weekDays = mContext.getResources().getStringArray(R.array.home_calendar_week);
    }

    public void updateData(List<FlashRequest.FlashData> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    public void appendData(List<FlashRequest.FlashData> data) {
        if (data == null || data.isEmpty()) {
            return;
        }
        if (this.mData == null) {
            mData = data;
        } else {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SimpleViewHolder(mContext.getLayoutInflater().inflate(R.layout.item_flash, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        // 填充数据
        SimpleViewHolder mHolder = (SimpleViewHolder) holder;
        FlashRequest.FlashData item = getItem(position);
        if (item == null) {
            return;
        }
        if (!TextUtils.isEmpty(item.content)) {
            CharSequence content = Html.fromHtml(item.content);
            mHolder.mTvMessage.setText(content);
        }
        // 显示时间
        mHolder.mTvTime.setText(item.time);

        String nextDate = getNextItemDate(position);
        if (nextDate != null && !item.date.equals(nextDate)) {
            mHolder.mTvLineBottom.setVisibility(View.INVISIBLE);
        } else {
            mHolder.mTvLineBottom.setVisibility(View.VISIBLE);
        }

        String previousDate = getPreviousItemDate(position);
        if (position == 0 || (previousDate != null && !item.date.equals(previousDate))) {
            mHolder.mTvLineTop.setVisibility(View.INVISIBLE);
            mHolder.mTvDate.setVisibility(View.VISIBLE);
            String dateText = item.date + " " + getWeekDay(item.week);
            mHolder.mTvDate.setText(dateText);
        } else {
            mHolder.mTvDate.setVisibility(View.GONE);
            mHolder.mTvLineTop.setVisibility(View.VISIBLE);
        }
    }

    private String getPreviousItemDate(int position) {
        if (position > 0) {
            FlashRequest.FlashData item = getItem(position - 1);
            if (item != null && !TextUtils.isEmpty(item.date)) {
                return item.date;
            }
        }
        return null;
    }

    private String getNextItemDate(int position) {
        FlashRequest.FlashData item = getItem(position + 1);
        if (item != null && !TextUtils.isEmpty(item.date)) {
            return item.date;
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    private FlashRequest.FlashData getItem(int position) {
        if (mData != null && mData.size() > position) {
            return mData.get(position);
        }
        return null;
    }

    private String getWeekDay(int week) {
        if (week <= 0 || week > 7) {
            return "";
        }
        return weekDays[week - 1];
    }

    public static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvTime;
        private TextView mTvDate;
        private TextView mTvMessage;
        private View mTvLineTop;
        private View mTvLineBottom;

        SimpleViewHolder(final View itemView) {
            super(itemView);
            mTvTime = itemView.findViewById(R.id.tv_time);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mTvMessage = itemView.findViewById(R.id.tv_message);
            mTvLineTop = itemView.findViewById(R.id.tv_line_top);
            mTvLineBottom = itemView.findViewById(R.id.tv_line_bottom);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  do nothing 。。。。配合 recycler view 中的 onInterceptTouchEvent 拦截消费事件，处理首页中的滑动逻辑
                }
            });
        }
    }

}
